package com.feng.zhihao.apigeteway.gateway.bean.impl.GENERATION_RULE;

import java.util.Optional;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Component;

import com.feng.zhihao.apigeteway.entity.FieldLimitInfo;
import com.feng.zhihao.apigeteway.entity.InterfaceFieldGenerationRule;
import com.feng.zhihao.apigeteway.entity.InterfaceFieldInfo;
import com.feng.zhihao.apigeteway.gateway.ReqContext;
import com.feng.zhihao.apigeteway.gateway.bean.GENERATION_RULE;

@Component("GENERATION_RULE_LONG")
public class GENERATION_RULE_LONG implements GENERATION_RULE{

	@Override
	public Object generationValue(InterfaceFieldInfo interfaceFieldInfo,
			InterfaceFieldGenerationRule rule, ReqContext reqContext) {
		Optional<FieldLimitInfo> limitInfo=interfaceFieldInfo.getFieldLimitInfos().stream().findFirst();
		if(limitInfo.isPresent()){
			if(limitInfo.get().getLimitMin() != null && limitInfo.get().getLimitMax() != null){
				return RandomUtils.nextLong(Long.parseLong(limitInfo.get().getLimitMin()),Long.parseLong(limitInfo.get().getLimitMax()));
			}
			if(limitInfo.get().getLimitMin() != null){
				return RandomUtils.nextLong(Long.parseLong(limitInfo.get().getLimitMin()),Long.MAX_VALUE);
			}
			return RandomUtils.nextLong(Long.MIN_VALUE,Long.parseLong(limitInfo.get().getLimitMax()));
		}else{
			return null;
		}
	}

}
